import MatchGameConfig from "./MatchGameConfig";
import BottomPrefabCtrl from "./BottomPrefabCtrl";
import MatchGameUtilsDataMgr from "./MatchGameUtilsDataMgr";
import frameworkManager from "./frameworkManager";

const { ccclass, property } = cc._decorator;

@ccclass
class BottomBgCtrl {
    static _instance = null;

    bottomItem = null;
    bottomBgParent = null;
    bottomNodeGrid = [];
    isReady = false;
    allBottomMaskArr = [];

    static getInstance():BottomBgCtrl {
        if (!this._instance) {
            this._instance = new BottomBgCtrl();
        }
        return this._instance;
    }

    async ready(e, t) {
        this.bottomItem = e;
        this.bottomBgParent = t;
        console.log("BottomBgCtrl---", MatchGameConfig.MAX_COL, MatchGameConfig.MAX_ROW);

        for (let a = 0; a < MatchGameConfig.MAX_ROW; a++) {
            this.bottomNodeGrid[a] = [];
            for (let o = 0; o < MatchGameConfig.MAX_COL; o++) {
                this.bottomNodeGrid[a][o] = null;
                let i = cc.instantiate(this.bottomItem);
                i.addComponent(BottomPrefabCtrl);
                i.parent = this.bottomBgParent;
                i.setPosition(MatchGameUtilsDataMgr.getPos(a, o));
                i.name = a + "_" + o;
                this.bottomNodeGrid[a][o] = i;
                i.active = false;
            }
        }

        this.isReady = true;
    }

    init() {
        // TODO: Add initialization code here
    }

    getBottomNodeGrid() {
        return this.bottomNodeGrid;
    }

    initBottomBgItem(e, t, a) {
        if (this.bottomNodeGrid[e][t]) {
            if (a[e][t] && a[e][t] === MatchGameConfig.DisplayMapType.Normal) {
                this.bottomNodeGrid[e][t].active = true;
                this.bottomNodeGrid[e][t].getComponent(BottomPrefabCtrl).init(e, t, a);
            } else {
                this.bottomNodeGrid[e][t].active = false;
            }
        }
    }

    recoverBottom() {
        // TODO: Add recoverBottom code here
    }
}

const instance = BottomBgCtrl.getInstance();
export default instance;
